package com.xiaobingby.easyuisys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xiaobingby.common.entity.EUDataGridQuery;
import com.xiaobingby.common.entity.EUDataGridResult;
import com.xiaobingby.common.utils.StringUtil;
import com.xiaobingby.easyuisys.service.IUserService;
import com.xiaobingby.sys.entity.User;
import com.xiaobingby.sys.mapper.UserMapper;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 李磊
 * @date 2019/5/5
 */
@Service("easyuiUserService")
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements IUserService {

    @Override
    public boolean userModifyPwd(Long id,String oldPwd,String pwd){
        User oldUser = this.baseMapper.selectById(id);
        SimpleHash oldPassword = new SimpleHash("MD5", oldPwd, oldUser.getSalt());
        if(!oldPassword.toString().equals(oldUser.getPassword())){
            return false;
        }

        String salt = String.valueOf(System.currentTimeMillis());
        SimpleHash password = new SimpleHash("MD5", pwd, salt);
        User user = this.baseMapper.selectById(id);
        user.setSalt(salt);
        user.setPassword(password.toString());
        this.baseMapper.updateById(user);
        return true;
    }

    @Override
    public EUDataGridResult listUser(EUDataGridQuery query, User user){
        Page<User> page = query.getPage();
        EntityWrapper<User> wrapper = new EntityWrapper<>();
        if (StringUtil.isNotEmpty(user.getUsername())) {
            wrapper.like("username", user.getUsername());
        }
        if (user.getStatus() != null) {
            wrapper.eq("status", user.getStatus());
        }
        Page<User> userPage = selectPage(page, wrapper);
        return EUDataGridResult.newInstance(userPage);
    }
}
